package ui.videos 
{
	import com.greensock.TweenMax;
	import com.video.VideoPlayerEvent;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.StageDisplayState;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import ui.videos.SeekAudio;
	import utils.ShapeUtil;
	
	/**
	 * ...
	 * @author usual
	 */
	public class Control extends MovieClip 
	{
		private var _separate1:Shape;
		private var _separate2:Shape;
		public var pauseBtn:PauseBtn;
		public var playBtn:PlayBtn;
		public var seekBar:SeekBar;
		public var timeText:TimeText
		public var fullSreenBtn:FullScreenBtn;
		public var seekAudio:SeekAudio;
		public var videoPlayer:*;
		public function Control() 
		{
			addEventListener(Event.ADDED_TO_STAGE, build);
		}
		
		public function resize():void 
		{

			seekAudio.x = 0;
			seekAudio.y = (playBtn.height / 2) - (seekAudio.height / 2);
			_separate1.x = (seekAudio.x + seekAudio.width) + 17;
			pauseBtn.x = (_separate1.x + _separate1.width) + 17;
			playBtn.x = (pauseBtn.x + pauseBtn.width + 7);
			seekBar.x = playBtn.x + playBtn.width + 12;
			seekBar.y = 4;
			seekBar.resize();
			timeText.x = seekBar.x + seekBar.width + 12;
			timeText.y = (playBtn.height/2) - (timeText.height/2)
			
			fullSreenBtn.y = (playBtn.height / 2) - (fullSreenBtn.height / 2);
			fullSreenBtn.x = timeText.x +timeText.width + 36;
			
			
			_separate2.x = timeText.x +timeText.width + 18;
			
			_separate1.y = (playBtn.height / 2) - (_separate1.height / 2);
			_separate2.y = (playBtn.height / 2) - (_separate2.height / 2);
			
			var centerX:int = int((stage.stageWidth / 2) - (this.width / 2));
			
			x = centerX;
			//TweenMax.to(this, .1, { x:centerX } );
		}
		
		public function update():void 
		{
			seekBar.videoPlayer = videoPlayer;
			seekAudio.videoPlayer = videoPlayer;
			videoPlayer.setVolume(seekAudio.volume);
		}
		
		private function build(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, build);
			addBtn();
			addSeparates();
			resize();
		}
		
		private function addSeparates():void 
		{
			_separate1 = ShapeUtil.getLineV(17, 0xffffff);
			_separate2 = ShapeUtil.getLineV(17, 0xffffff);
			
			_separate1.alpha = .5;
			_separate2.alpha =  .5;
			
			addChild(_separate1)
			addChild(_separate2)
		}
		
		private function addBtn():void 
		{
			
			seekAudio = new SeekAudio();
			pauseBtn = new PauseBtn();
			playBtn = new PlayBtn();
			seekBar = new SeekBar();
			seekAudio.videoPlayer = videoPlayer;
			seekBar.videoPlayer = videoPlayer;
			timeText = new TimeText();
			fullSreenBtn = new FullScreenBtn();
			pauseBtn.buttonMode = playBtn.buttonMode =  fullSreenBtn.buttonMode = true;
			seekBar.addEventListener(VideoPlayerEvent.VIDEO_PAUSE, pausePlayerBySeekBar);
			seekBar.addEventListener(VideoPlayerEvent.VIDEO_PLAY, playPlayerBySeekBar);
			pauseBtn.addEventListener(MouseEvent.CLICK, pausePlayer);
			playBtn.addEventListener(MouseEvent.CLICK, playPlayer);
			fullSreenBtn.addEventListener(MouseEvent.CLICK, fullscreenToggle);
			addChild(seekAudio);
			addChild(pauseBtn);
			addChild(playBtn);
			addChild(seekBar);
			addChild(timeText);
			addChild(fullSreenBtn);
			
			
		}
		
		private function playPlayerBySeekBar(e:VideoPlayerEvent):void 
		{
			dispatchEvent(new VideoPlayerEvent(VideoPlayerEvent.VIDEO_PLAY));
		}
		
		private function pausePlayerBySeekBar(e:VideoPlayerEvent=null):void 
		{
			dispatchEvent(new VideoPlayerEvent(VideoPlayerEvent.VIDEO_PAUSE));
		}
		
		private function fullscreenToggle(e:MouseEvent):void 
		{
			if (stage.displayState == StageDisplayState.NORMAL)
			{
				stage.displayState = StageDisplayState.FULL_SCREEN;
			}
			else if (stage.displayState == StageDisplayState.FULL_SCREEN)
			{
				stage.displayState = StageDisplayState.NORMAL;
			}
			
		}
		private function playPlayer(e:MouseEvent=null):void 
		{
			dispatchEvent(new VideoPlayerEvent(VideoPlayerEvent.VIDEO_PLAY));
		}
		
		private function pausePlayer(e:MouseEvent=null):void 
		{
			dispatchEvent(new VideoPlayerEvent(VideoPlayerEvent.VIDEO_PAUSE));
		}
		
	}

}